package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.filecenter.annotations.NeedRole;
import com.bwda.dsrs.filecenter.aspect.LogTypeEnum;
import com.bwda.dsrs.filecenter.aspect.Loggable;
import com.bwda.dsrs.filecenter.domain.condition.terminalupgraderecord.TerminalUpgradeRecordCondition;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.filecenter.domain.form.terminalupgraderecord.TerminalUpgradeRecordCreateForm;
import com.bwda.dsrs.filecenter.domain.form.terminalupgraderecord.TerminalUpgradeRecordQueryForm;
import com.bwda.dsrs.filecenter.domain.form.terminalupgraderecord.TerminalUpgradeRecordUpdateForm;
import com.bwda.dsrs.filecenter.domain.po.terminalupgraderecord.TerminalUpgradeRecordPo;
import com.bwda.dsrs.filecenter.domain.vo.terminalupgraderecord.TerminalUpgradeRecordVo;
import com.bwda.dsrs.filecenter.service.TerminalUpgradeRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 终端升级记录
 *
 * @author yangjun
 */
@RestController
@RequestMapping("/b/terminalupgraderecord")
@Api(value = "/b/terminalupgraderecord", tags = "终端升级记录")
public class TerminalUpgradeRecordController extends BaseController {

    @Autowired
    private TerminalUpgradeRecordService terminalUpgradeRecordService;
    @NeedRole("sysadminrole")
    @ApiOperation(value = "查询终端升级记录", notes = "查询终端升级记录", httpMethod = "GET")
    @Loggable(descp = "查询终端升级记录", type = LogTypeEnum.QUERY, include = "")
    @RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<TerminalUpgradeRecordVo> query(@ApiParam(value = "ID主键", required = true) @RequestParam Integer id) throws BusinessServiceException {
        TerminalUpgradeRecordPo po = terminalUpgradeRecordService.queryWithValid(id);
        TerminalUpgradeRecordVo vo = CopyUtil.transfer(po, TerminalUpgradeRecordVo.class);
        return getSuccessResult(vo);
    }
    @NeedRole("sysadminrole")
    @ApiOperation(value = "查询终端升级记录数量", notes = "查询终端升级记录数量", httpMethod = "GET")
    @Loggable(descp = "查询终端升级记录数量", type = LogTypeEnum.QUERY, include = "")
    @RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<Integer> queryCount(@ModelAttribute @Valid TerminalUpgradeRecordQueryForm form) throws BusinessServiceException {
        TerminalUpgradeRecordCondition condition = this.getConditionByQueryForm(form);
        int count = terminalUpgradeRecordService.queryCount(condition);
        return getSuccessResult(count);
    }
    @NeedRole("sysadminrole")
    @ApiOperation(value = "查询终端升级记录列表", notes = "查询终端升级记录列表", httpMethod = "GET")
    @Loggable(descp = "查询终端升级记录列表", type = LogTypeEnum.QUERY, include = "")
    @RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<CentreListResponse<TerminalUpgradeRecordVo>> queryList(@ModelAttribute @Valid TerminalUpgradeRecordQueryForm form) throws BusinessServiceException {
        TerminalUpgradeRecordCondition condition = this.getConditionByQueryForm(form);
        List<TerminalUpgradeRecordPo> poList = terminalUpgradeRecordService.queryList(condition);
        List<TerminalUpgradeRecordVo> voList = CopyUtil.transfer(poList, TerminalUpgradeRecordVo.class);
        return getSuccessResult(getListResponse(voList));
    }
    @NeedRole("sysadminrole")
    @ApiOperation(value = "查询终端升级记录列表(带分页)", notes = "查询终端升级记录列表(带分页)", httpMethod = "GET")
    @Loggable(descp = "查询终端升级记录列表(带分页)", type = LogTypeEnum.QUERY, include = "")
    @RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<CentreCutPageResponse<TerminalUpgradeRecordVo>> queryPageList(@ModelAttribute @Valid TerminalUpgradeRecordQueryForm form) throws BusinessServiceException {
        TerminalUpgradeRecordCondition condition = this.getConditionByQueryForm(form);
        List<TerminalUpgradeRecordVo> voList = new ArrayList<>();
        int count = terminalUpgradeRecordService.queryCount(condition);
        if (count > 0) {
            List<TerminalUpgradeRecordPo> poList = terminalUpgradeRecordService.queryList(condition);
            voList = CopyUtil.transfer(poList, TerminalUpgradeRecordVo.class);
        }
        return getSuccessResult(getPageResponse(form, count, voList));
    }
    @NeedRole("sysadminrole")
    @ApiOperation(value = "新增终端升级记录", notes = "新增终端升级记录", httpMethod = "POST")
    @Loggable(descp = "新增终端升级记录", type = LogTypeEnum.CREATE, include = "")
    @RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<TerminalUpgradeRecordVo> add(@ModelAttribute @Valid TerminalUpgradeRecordCreateForm form) throws BusinessServiceException {
        TerminalUpgradeRecordPo po = CopyUtil.transfer(form, TerminalUpgradeRecordPo.class);
        terminalUpgradeRecordService.insert(po);
        TerminalUpgradeRecordVo vo = CopyUtil.transfer(po, TerminalUpgradeRecordVo.class);
        return getSuccessResult(vo);
    }
    @NeedRole("sysadminrole")
    @ApiOperation(value = "修改终端升级记录", notes = "修改终端升级记录", httpMethod = "POST")
    @Loggable(descp = "修改终端升级记录", type = LogTypeEnum.UPDATE, include = "")
    @RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity update(@ModelAttribute @Valid TerminalUpgradeRecordUpdateForm form) throws BusinessServiceException {
        TerminalUpgradeRecordPo po = CopyUtil.transfer(form, TerminalUpgradeRecordPo.class);
        terminalUpgradeRecordService.update(po);
        return getSuccessResult();
    }
    @NeedRole("sysadminrole")
    @ApiOperation(value = "删除终端升级记录", notes = "删除终端升级记录", httpMethod = "POST")
    @Loggable(descp = "删除终端升级记录", type = LogTypeEnum.DELETE, include = "")
    @RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity delete(@ApiParam(value = "ID主键", required = true) @RequestParam Integer id) throws BusinessServiceException {
        terminalUpgradeRecordService.delete(id);
        return getSuccessResult();
    }

    /**
     * TerminalUpgradeRecordQueryForm转换为TerminalUpgradeRecordCondition
     *
     * @param form
     * @return
     */
    private TerminalUpgradeRecordCondition getConditionByQueryForm(TerminalUpgradeRecordQueryForm form) {
        TerminalUpgradeRecordCondition condition = CopyUtil.transfer(form, TerminalUpgradeRecordCondition.class);
        return condition;
    }

}